package cn.lh.sys.controller;

import cn.lh.basic.util.JsonResult;
import cn.lh.basic.util.Page;
import cn.lh.sys.query.DictionaryDetailQuery;
import cn.lh.sys.domain.DictionaryDetail;
import cn.lh.sys.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dictionaryDetail")
@RestController
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping("/{id}")
    public DictionaryDetail findOne(@PathVariable("id") long id) {
        return dictionaryDetailService.findById(id);
    }

    @GetMapping
    public List<DictionaryDetail> findAll() {
        return dictionaryDetailService.findAll();
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") long id) {
        try {
            dictionaryDetailService.del(id);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }

    }

    @PutMapping
    public JsonResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return JsonResult.set(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "操作失败");
        }

    }

    @PostMapping
    public Page<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query) {
        return dictionaryDetailService.queryPage(query);
    }

    @PatchMapping
    public JsonResult batchDelete(@RequestBody Long[] ids){
        try {
            dictionaryDetailService.batchDelete(ids);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }
    }

}
